package com.hxd.mall.widget;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

/**
 * @Describe
 * @Date : 2018/11/4
 * @Email : zhaoyong@entgroup.com
 * @Author : zhaoyong
 */
public class GridItemDecoration extends RecyclerView.ItemDecoration {

    private int rowSpace;

    private int columnSpace;

    private int spanCount;

    public GridItemDecoration(int rowSpace, int columnSpace, int spanCount) {
        this.rowSpace = rowSpace;
        this.columnSpace = columnSpace;
        this.spanCount = spanCount;
    }

    public GridItemDecoration(int space, int spanCount) {
        this.rowSpace = space;
        this.columnSpace = space;
        this.spanCount = spanCount;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        //不是第一个的格子都设一个左边和底部的间距
        outRect.left = columnSpace;
        outRect.bottom = rowSpace;
        //由于每行都只有3个，所以第一个都是3的倍数，把左边距设为0
        if (parent.getChildLayoutPosition(view) % spanCount == 0) {
            outRect.left = 0;
        }
    }
}
